#include "mygl.h"
#include <iostream>
#include <GL/glu.h>
#include <string.h>

MyGLWidget::MyGLWidget(QWidget *parent)
                        : QGLWidget(parent)
{setFocusPolicy(Qt::StrongFocus);
    zoom=25;
    sep=14;
 p=new PilaT();
 ps=new pilaSend();
  p2=new PilaT();
 ps2=new pilaSend();
//p->crearMalla(0);
//p2->crearMalla(7);
 p->readFromFile("malla0.txt");
 p2->readFromFile("malla1.txt");
}

MyGLWidget::~MyGLWidget()
{
}




void MyGLWidget::initializeGL()
{

// Background color used when glClear clears the gl widget.
        glClearColor(1.0f, 1.0f, 1.0f, 1.0f);

        // Shading interpolation.
        glShadeModel(GL_SMOOTH); // Interpolate colors.
        //glShadeModel(GL_FLAT); // Last color is used.

        // Specific hints.
        glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();



    }
void MyGLWidget::paintGL()
{

        // Clear the actual image, color and depth buffers
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        // Load the identity into the model's transformation matrix.
        glLoadIdentity();

/*glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();*/


    glTranslatef(0.0, 0.0, -zoom);
 // glRotatef(-35.0, 1, 0, 0);
  //  glRotatef(0.0, 0, 1, 0);
 // glRotatef(90.0, 0, 0, 1);




glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glPushMatrix();
  glTranslatef(-15.0,-8.0,0.0);

 glScalef(2.0,2.0,1.0);

//p->dibujar();

/*t[0]->lepp(p);
;*/

p->dibujar();
glPopMatrix();
glPushMatrix();
 glTranslatef(-sep,-8,0.0);
  glScalef(2.0,2.0,1.0);
 p2->dibujar();
glPopMatrix();
 // std::cout << "Test\n"<<  t[0]->ladoMayor()<<"eso";




}

void MyGLWidget::resizeGL(int w, int h)
{
     GLfloat fAspect;

    // Set Viewport to window dimensions
    glViewport(0, 0, w, h);

    // Calculate aspect ratio of the window
    fAspect = (GLfloat)w/(GLfloat)h;

    // Set the perspective coordinate syst

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    //glTranslatef(0.0f, 0.0f, 150.0f);
    // field of view of 45 degrees, near and far planes 1.0 and 425
    gluPerspective(45.0f, fAspect, 1.0, 1000.0);

    // Modelview matrix reset
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

void MyGLWidget::refinar2(){

for(int i=0;i<50;i++){
 triangulo* t1;
       t1=p2->getrandom();
       //std::cout << "vertice"<<  t1->getvertices(1,1)  <<"eso\n";
       t1->lepp(p2,ps2);
        //t1=p2->getrandom();
        //t1->lepp(p2,ps2);
   }
  // t[0]->lepp(p);
      // std::cout << "maxindex"<< p->maxindex  <<"eso\n";
        updateGL();
    }
void MyGLWidget::refinar1(){

    for(int i=0;i<50;i++){
 triangulo* t1;
       t1=p->getrandom();
       //std::cout << "vertice"<<  t1->getvertices(1,1)  <<"eso\n";
       t1->lepp(p,ps);
        //t1=p2->getrandom();
        //t1->lepp(p2,ps2);
   }
  // t[0]->lepp(p);
      // std::cout << "maxindex"<< p->maxindex  <<"eso\n";
        updateGL();
}
void MyGLWidget::sincronizar1(){

   p->refinarPedidos(ps2,ps);
       // ps->vaciar();
        updateGL();
}
void MyGLWidget::sincronizar2(){//p2->recibirPunto(ps2,pun2);
        p2->refinarPedidos(ps,ps2);
       // ps->vaciar();
        updateGL();}
void MyGLWidget::separar(){ sep--;
    updateGL();
}
void MyGLWidget::unir(){if(sep<15) sep++;
    updateGL();
}
void MyGLWidget::printToFile(){
    p->printToFile("malla.txt");
}
